package com.qlyl.modules.information.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qlyl.common.api.vo.Result;
import com.qlyl.common.constant.CommonCheckStatus;
import com.qlyl.common.util.oConvertUtils;
import com.qlyl.modules.information.entity.AppUser;
import com.qlyl.modules.information.entity.Consumers;
import com.qlyl.modules.information.mapper.ConsumersMapper;
import com.qlyl.modules.information.service.IAppApiService;
import com.qlyl.modules.information.service.IConsumersService;
import com.qlyl.modules.information.service.IEthService;
import com.senlin.ethereum.core.EthAccount;
import com.senlin.ethereum.core.EthClient;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.web3j.crypto.Credentials;

@Service
public class ConsumersServiceImpl extends ServiceImpl<ConsumersMapper, Consumers> implements IConsumersService {

    @Autowired
    private EthClient ethClient;
    @Autowired
    private IEthService ethService;
    @Autowired
    private IAppApiService appApiService;

    @Override
    @Transactional
    public boolean frozenBatchChcek(String ids, String checkStatus) {
            String[] arr = ids.split(",");
            for (String id : arr) {
                if (oConvertUtils.isNotEmpty(id)) {
                    Consumers consumers = getById(id);
                    if(CommonCheckStatus.ON_CHAIN.equals(checkStatus)){
                        if(consumers.getCheckStatus() !=2 ||consumers.getBindingStatus()!=1|| StringUtils.isBlank(consumers.getBindingUserid())){
                            throw new RuntimeException("该上链企业暂未通过企业信息审核或者暂未绑定用户！！");
                        }

                        String userid = consumers.getBindingUserid();
                        AppUser appUser = appApiService.getById(userid);
                        //确认上链之后发起交易
                        String dataString = consumersToJsonchain(consumers).toString();

                        EthClient.EthWallet ethWallet = ethClient.ethWallet(new EthAccount(
                                "balance stool bar stool correct dolphin inherit grass dawn snack appear finish"
                        ));
                        // 获取钱包地址
                        String address = ethWallet.loadCredentialsAndGet(Credentials::getAddress);
                        Result result = ethService.transferTo(appUser,address,dataString);

                        if (!result.isSuccess()){
                            throw new RuntimeException(result.getMessage());
                        }
                    }

                    if(CommonCheckStatus.WAITTING_CHAIN.equals(checkStatus)){
                        if(consumers.getBindingStatus()!=1|| StringUtils.isBlank(consumers.getBindingUserid())){
                            throw new RuntimeException("该企业暂未被认领，请先认证绑定用户！");
                        }
                    }
                    this.update(new Consumers().setCheckStatus(Integer.valueOf(checkStatus)),
                            new UpdateWrapper<Consumers>().lambda().eq(Consumers::getId, id));
                }
            }
            return true;
    }

    @Override
    public JSONObject consumersToJsonchain(Consumers consumers) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("companyName",consumers.getCompanyName());
        jsonObject.put("legalPersonInfo",consumers.getLegalPersonInfo());
        jsonObject.put("creditCode",consumers.getCreditCode());
        jsonObject.put("registeredCapital",consumers.getRegisteredCapital());
        jsonObject.put("companyType",consumers.getCompanyType());
        jsonObject.put("establishDate",consumers.getEstablishDate());
        jsonObject.put("businessTerm",consumers.getBusinessTerm());
        jsonObject.put("registrationStatus",consumers.getRegistrationStatus());
        jsonObject.put("registrationAuthority",consumers.getRegistrationAuthority());
        jsonObject.put("approvalDate",consumers.getApprovalDate());
        jsonObject.put("address",consumers.getAddress());
        jsonObject.put("businessScope",consumers.getBusinessScope());
        return jsonObject;
    }







}
